package cn.mumzone.pos.presenter;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.PatternUtils;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.event.SettleSuccessEvent;
import cn.mumzone.pos.model.MemberEntity;
import cn.mumzone.pos.model.ReserveOrderEntity;
import cn.mumzone.pos.model.ServiceEntity;
import cn.mumzone.pos.model.ShopUsableInfoEntity;
import cn.mumzone.pos.model.source.MemberRepository;
import cn.mumzone.pos.model.source.ReserveRepository;
import cn.mumzone.pos.presenter.contract.AddServeContract;
import cn.mumzone.pos.util.AppUtil;
import rx.Subscription;
import rx.functions.Action1;

/**
 * Created By Dlx
 * on 2019/7/9
 */
public class AddServePresenter extends AbsPresenter<AddServeContract.View, ReserveRepository> implements AddServeContract.Presenter {

    private MemberRepository memberRepo;

    @Override
    protected void initOtherRepository() {
        super.initOtherRepository();
        memberRepo = new MemberRepository();
    }

    public void initEvent() {
        Subscription settleListener = RxBus.getDefault()
                .toObservable(SettleSuccessEvent.class)
                .subscribe(new Action1<SettleSuccessEvent>() {
                    @Override
                    public void call(SettleSuccessEvent settleSuccessEvent) {
                        mView.showSettleSuccess();
                    }
                });
        addSubscrebe(settleListener);
    }

    @Override
    public void getServeList(String memberId) {
        ApiCallBack<ShopUsableInfoEntity> callBack = new ApiCallBack<ShopUsableInfoEntity>() {
            @Override
            public void onSuccess(ShopUsableInfoEntity usableInfoEntity) {
                mView.showServeList(usableInfoEntity.getServices());
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };
        mRepo.getShopUsableServiceList(memberId, callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void getServeListWithMemberId(String memberId) {
        ApiCallBack<ShopUsableInfoEntity> callBack = new ApiCallBack<ShopUsableInfoEntity>() {
            @Override
            public void onSuccess(ShopUsableInfoEntity usableInfoEntity) {
                mView.showServeWithCardList(usableInfoEntity.getServices());
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };
        mRepo.getShopUsableServiceList(memberId, callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void doAddReserve(String memberId, String username, String phone, String appointmentTime, List<ServiceEntity> serviceList) {
        if (ListUtil.isListEmpty(serviceList)) {
            mView.showError("请先添加服务");
            return;
        }
        ApiCallBack<ReserveOrderEntity> callBack = new ApiCallBack<ReserveOrderEntity>() {
            @Override
            public void onSuccess(ReserveOrderEntity reserveOrderEntity) {
                mView.showAddReserveSuccess(reserveOrderEntity);
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };


        mRepo.addReserve(AppUtil.hasServiceBusiness() ? ReserveOrderEntity.ORDER_TYPE_RESERVE
                        : ReserveOrderEntity.ORDER_TYPE_BABYSWIM, ReserveOrderEntity.ORDER_STATUS_RESERVED,
                AppDateUtil.date2TimeStamp(appointmentTime, AppDateUtil.YYYY_MM_DD_HH_MM), memberId, username, phone, serviceList, callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void searchMember(String phone) {
        if (!PatternUtils.isMobileNO(phone)) {
            mView.showError("手机号格式不正确");
            return;
        }

        ApiCallBack<MemberEntity> callBack = new ApiCallBack<MemberEntity>() {
            @Override
            public void onSuccess(MemberEntity memberEntity) {
                mView.onSearchMemberSuccess(memberEntity);
            }

            @Override
            public void onError(BaseResponse response) {
                mView.onNoMemberFound();
            }
        };
        memberRepo.getMemberInfoByPhone(phone, callBack);
        addSubscrebe(callBack);
    }
}
